package com.kbbywyclient.ui.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.ErrorCorrectionModule;
import com.kbbywyclient.presenter.ErrorCorrectionPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.ErrorCorrectionContarct;
import com.kbbywyclient.util.SoftKeyboardUtils;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomPopwin;
import com.kbbywyclient.widget.CustomToolBar;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 * Created by FHW
 * on 2017/5/11
 */

public class ErrorCorrectionActivity extends BaseActivity implements ErrorCorrectionContarct.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;
    @BindView(R.id.et_content)
    EditText et_content;

    private String productLoansID = "";

    private int mType = 0;//0 -我的产品   1-产品库

    private String errorContent = "";

    @Inject
    ErrorCorrectionPresenter errorCorrectionPresenter;
    private CustomPopwin popwin;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_error_correction;
    }

    @OnClick({R.id.tv_determine})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_determine:
                if (StringUtil.isNotEmpty(errorContent.trim())) {
                    errorCorrectionPresenter.submitError(PreUtils.getUser(this).getSalesID() + "", productLoansID, errorContent);
                } else {
                    ToastUtil.showShort("说点什么吧");
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("信息纠错");
    }

    @Override
    protected void initView() {
        productLoansID = getIntent().getStringExtra("productLoansID");
        mType = getIntent().getIntExtra("mType", 0);

        v_toolbar.setLeftViewOnClickLinster(v -> finish());
        v_toolbar.setRightViewOnClickLinster(v -> finish());

        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                errorContent = s.toString();
            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getErrorCorrectionComponent(new ErrorCorrectionModule(this)).inject(this);
    }

    private void showPopView() {

        View proView = LayoutInflater.from(this).inflate(R.layout.pop_error_correction, null, false);

        RelativeLayout rl_all = (RelativeLayout) proView.findViewById(R.id.rl_all);
        TextView tv_determine = (TextView) proView.findViewById(R.id.tv_determine);
        ImageView iv_colse = (ImageView) proView.findViewById(R.id.iv_colse);

        if (popwin == null) {
            popwin = new CustomPopwin(this, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.6f);
        }

        popwin.setContentView(proView);
        popwin.showAtLocation(proView, Gravity.CENTER, 0, 0);
        popwin.setOutsideTouchable(true);
        popwin.setFocusable(true);

        iv_colse.setOnClickListener(v -> {
            popwin.dismiss();
            finish();
        });
        tv_determine.setOnClickListener(v -> {
            popwin.dismiss();
            finish();
        });
        rl_all.setOnClickListener(v -> {
        });

    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucError(BaseModel baseModel) {
        SoftKeyboardUtils.hideSoftKeyboard(et_content);
        showPopView();
    }

    @Override
    public void loadFaiError(String errorMsg) {

    }
}
